import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression

lines = np.loadtxt('../data/USA_Housing.csv', delimiter=',', dtype='str')
print(lines.shape)
header = lines[0]
lines = lines[1:].astype(float)

# 训练集和测试集
ratio = 0.8
split = int(len(lines) * ratio)
lines = np.random.permutation(lines)
train, test = lines[:split], lines[split:]

# 标准化  1 2 3 4 5 均值：3 方差：q
# 均值为0，方差为1的数据 -1.414 -0.707 0 0.707 1.414

scaler = StandardScaler()
scaler.fit(train)
train = scaler.transform(train)
test = scaler.transform(test)

x_train, y_train = train[:,:-1], train[:,-1]
x_test, y_test = test[:,:-1], test[:,-1]

linreg = LinearRegression()
linreg.fit(x_train, y_train)
print("回归系数",linreg.coef_,linreg.intercept_)
y_pred = linreg.predict(x_test)
print(y_pred)
#  [0.65916839 0.46980208 0.34697857 0.00670016 0.42986258] 1.0740305524853286e-14
# [ 6.46003124e-01  4.60869114e-01  3.42688977e-01  3.47152856e-03
#   4.27493021e-01 -2.24265051e-14]